package com.example.hospitaldemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hospitaldemo.entity.Zhiliaoshi;
import com.example.hospitaldemo.entity.CheckInRecord;
import com.example.hospitaldemo.entity.Patient;
import com.example.hospitaldemo.service.IZhiliaoshiService;
import com.example.hospitaldemo.service.ICheckInRecordService;
import com.example.hospitaldemo.service.IPatientService;
import com.example.hospitaldemo.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2024-12-16
 */
@RestController
@RequestMapping("/checkInRecord")
public class CheckInRecordController {
    @Autowired
    private IPatientService patientService;
    @Autowired
    private ICheckInRecordService checkInRecordService;
    @Autowired
    private IZhiliaoshiService zhiliaoshiService;
    /**
     * 治疗，保存新的病人信息
     */
    @PostMapping("save")
    public ResultData save(@RequestBody CheckInRecord obj){
        //1.先获取病人信息
        Patient patient = obj.getPatient();
        //2.保存病人信息
        boolean b1 = patientService.save(patient);
        //3.保存病人的治疗信息
        obj.setCheckInTime(LocalDateTime.now());
        obj.setPatientId(patient.getId());
        boolean b2 = checkInRecordService.save(obj);
        //4.修改治疗室信息
        Zhiliaoshi zhiliaoshi = new Zhiliaoshi();
        zhiliaoshi.setId(obj.getZhiliaoshiId());
        zhiliaoshi.setState(1);
        boolean b3 = zhiliaoshiService.updateById(zhiliaoshi);
        if (b1 && b2 && b3){
            return ResultData.success();
        }
        return ResultData.error(101,"操作失败!!");
    }

    /**
     * 治疗结束登记
     */
    @GetMapping("getByZhiliaoshiId")
    public ResultData getByZhiliaoshiId(String zhiliaoshiId){
        QueryWrapper<CheckInRecord> queryWrapper = new QueryWrapper();
        queryWrapper.eq("zhiliaoshi_id",zhiliaoshiId);
        queryWrapper.isNull("check_out_time");
        CheckInRecord one = checkInRecordService.getByZhiliaoshiId(queryWrapper);

        if (one != null){
          return ResultData.success(one);
        }

        return ResultData.error(101,"该治疗室是空治疗室!!");
    }
}
